package cn.fiker.lib.pageBar;

import java.util.HashSet;
import java.util.Iterator;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.fiker.examples.pager.R;

public class PageBarView extends LinearLayout implements OnClickListener {

	private ImageButton btPrevPage, btNextPage, btFirstPage, btLastPage;
	private TextView tPageCount, tPerPage, tCurrentPage, tPageInfo;
	private LayoutInflater mInflater;
	private LinearLayout layout;

	private int pageCount = 0, perPage = 10;
	private int itemCount = 0, currentPage = 1, startIndex = 0;

	protected HashSet<OnPagedListener> listeners = new HashSet<OnPagedListener>();
	private pageEvent pEvent = null;

	public PageBarView(Context context) {
		this(context, null);
	}

	public PageBarView(Context context, AttributeSet attrs) {
		super(context, attrs);

		mInflater = LayoutInflater.from(context);
		layout = (LinearLayout) mInflater.inflate(R.layout.bar_pager, null);

		btFirstPage = (ImageButton) layout.findViewById(R.id.pager_first);
		btFirstPage.setOnClickListener(this);

		btPrevPage = (ImageButton) layout.findViewById(R.id.pager_prev);
		btPrevPage.setOnClickListener(this);

		btNextPage = (ImageButton) layout.findViewById(R.id.pager_next);
		btNextPage.setOnClickListener(this);

		btLastPage = (ImageButton) layout.findViewById(R.id.pager_last);
		btLastPage.setOnClickListener(this);

		tPageCount = (TextView) layout.findViewById(R.id.pager_count);
		tPerPage = (TextView) layout.findViewById(R.id.pager_per);
		tCurrentPage = (TextView) layout.findViewById(R.id.pager_current);
		tPageInfo = (TextView) layout.findViewById(R.id.pager_info);
	}

	public void addPagedListener(OnPagedListener listener) {
		if (listeners == null) {
			listeners = new HashSet<OnPagedListener>();
		}
		listeners.add(listener);
	}

	public void removePagedListtener(OnPagedListener listener) {
		if (listeners == null) {
			return;
		}
		listeners.remove(listener);

		pEvent = new pageEvent(this);
		notifyListeners(pEvent);
	}

	public void notifyListeners(pageEvent e) {
		Iterator<OnPagedListener> iter = listeners.iterator();
		while (iter.hasNext()) {
			OnPagedListener listener = (OnPagedListener) iter.next();
			e.onPageEvent(e);
			switch (e.getEventCode()) {
			case pageEvent.EVENT_PAGE_FIRST:
				listener.onFirstPage(e);
				break;
			case pageEvent.EVENT_PAGE_PREV:
				listener.onPrevPage(e);
				break;
			case pageEvent.EVENT_PAGE_NEXT:
				listener.onNextPage(e);
				break;
			case pageEvent.EVENT_PAGE_LAST:
				listener.onLastPage(e);
				break;
			case pageEvent.EVENT_PAGE_GOTO:
				listener.onGotoPage(e, e.getEventPage());
				break;
			}
		}
	}

	@Override
	protected void onFinishInflate() {
		measurePage();
		addView(layout, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		super.onFinishInflate();
	}

	public void measurePage() {
		measurePageCount();
	}

	public void reMeasure() {
		setPageSize(perPage);
		setCount(itemCount);
		measurePageCount();
	}

	public void setPageSize(int pageSize) {
		this.perPage = pageSize;
		measurePage();
	}

	public int getPageSize() {
		return perPage;
	}

	public void setCount(int itemCount) {
		this.itemCount = itemCount;
		measurePage();
	}

	public int getPageCount() {
		return pageCount;
	}

	public int getCurrentPage() {
		return this.currentPage;
	}

	public int getStartIndex() {
		return this.startIndex;
	}

	private void nextPage() {
		if (currentPage < pageCount) {
			currentPage++;
		} else {
			currentPage = pageCount;
		}
		measurePage();
		if (listeners == null) {
			return;
		} else {
			pEvent = new pageEvent(this);
			pEvent.onNextPage(pEvent);
			notifyListeners(pEvent);
		}
	}

	private void prevPage() {
		if (currentPage > 0) {
			currentPage--;
		} else {
			currentPage = 1;
		}
		measurePage();
		if (listeners == null) {
			return;
		} else {
			pEvent = new pageEvent(this);
			pEvent.onPrevPage(pEvent);
			notifyListeners(pEvent);
		}
	}

	private void lastPage() {
		currentPage = pageCount;
		measurePage();
		if (listeners == null) {
			return;
		} else {
			pEvent = new pageEvent(this);
			pEvent.onLastPage(pEvent);
			notifyListeners(pEvent);
		}
	}

	private void firstPage() {
		currentPage = 1;
		measurePage();
		if (listeners == null) {
			return;
		} else {
			pEvent = new pageEvent(this);
			pEvent.onFirstPage(pEvent);
			notifyListeners(pEvent);
		}
	}

	private void display() {
		if (this.itemCount < this.perPage || this.pageCount == 1) {
			layout.setVisibility(INVISIBLE);
			return;
		} else {
			layout.setVisibility(VISIBLE);
		}

		if (currentPage <= 1) {
			btFirstPage.setVisibility(INVISIBLE);
			btPrevPage.setVisibility(INVISIBLE);
			btNextPage.setVisibility(VISIBLE);
			btLastPage.setVisibility(VISIBLE);
		} else if (currentPage < pageCount) {
			btFirstPage.setVisibility(VISIBLE);
			btLastPage.setVisibility(VISIBLE);
			btNextPage.setVisibility(VISIBLE);
			btPrevPage.setVisibility(VISIBLE);
		} else if (currentPage == pageCount) {
			btFirstPage.setVisibility(VISIBLE);
			btPrevPage.setVisibility(VISIBLE);
			btNextPage.setVisibility(INVISIBLE);
			btLastPage.setVisibility(INVISIBLE);
		}

		tCurrentPage.setText("" + currentPage);
		tPageCount.setText("" + pageCount);
		tPerPage.setText("" + perPage);
		tPageInfo.setText("每页 " + perPage + " 条 共 " + pageCount + " 页");

		Log.d("cn.fiker.lib.pageBar", "view.PageBarView ： size:" + perPage + " itemCount:" + itemCount
				+ " pageCount:" + pageCount + " current:" + currentPage);
	}

	private void measurePageCount() {
		if (itemCount < perPage) {
			pageCount = 1;
		} else {
			if (itemCount % perPage == 0) {
				pageCount = (int) (itemCount / perPage);
			} else {
				pageCount = (int) (itemCount / perPage) + 1;
			}
		}
		startIndex = (currentPage - 1) * perPage;
		if (currentPage > pageCount) {
			firstPage();
		}
		display();
	}

	public void onClick(View v) {
		if (v.getId() == R.id.pager_info) {
		} else if (v.getId() == R.id.pager_first) {
			firstPage();
		} else if (v.getId() == R.id.pager_prev) {
			prevPage();
		} else if (v.getId() == R.id.pager_next) {
			nextPage();
		} else if (v.getId() == R.id.pager_last) {
			lastPage();
		}
	}
}
